package com.diudiutechnology.www.yadiudiu.yadiudiu.singlecall.MyActivity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.diudiutechnology.www.yadiudiu.R;
import com.diudiutechnology.www.yadiudiu.util.utils.Constant;
import com.diudiutechnology.www.yadiudiu.util.utils.MyProgressDialog;
import com.diudiutechnology.www.yadiudiu.util.utils.ToastUtils;
import com.diudiutechnology.www.yadiudiu.util.view.pulltorefresh.PullToRefreshBase;
import com.diudiutechnology.www.yadiudiu.util.view.pulltorefresh.PullToRefreshListView;
import com.diudiutechnology.www.yadiudiu.yadiudiu.main.MyApplication;
import com.diudiutechnology.www.yadiudiu.yadiudiu.singlecall.CleanBusiness;
import com.diudiutechnology.www.yadiudiu.yadiudiu.singlecall.MyAdapter.CleaningActivityAdapter;

import org.xutils.common.Callback;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

import static com.diudiutechnology.www.yadiudiu.R.id.listview;

/**
 * Created by Administrator on 2017/8/21.
 * 0、苦力  1、干洗 2、开锁 3、家政
 */

public class CleaningActivity extends Activity implements View.OnClickListener, Callback.CommonCallback<String> {
    public static final String CLEANING_BEAN = "cleaning_bean";
    @Bind(R.id.retur)
    LinearLayout retur;
    @Bind(listview)
    PullToRefreshListView mPullToRefreshListView;
    @Bind(R.id.title_center)
    TextView title_center;
    @Bind(R.id.search_ly)
    LinearLayout search_ly;
    private String pos = "0";
    private List<CallOrderBean> mListBean = new ArrayList<>();
    private CleaningActivityAdapter mAdapter;
    // 请求数据
    private int mPageIndex = 1;
    private boolean mIsStart = true;//判断是否上下加载 true:下拉刷新  false:上拉加载数据
    private MyProgressDialog mDialog;
    private ListView mListView;
    private String mApi;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cleaning);
        ButterKnife.bind(this);
        mDialog = new MyProgressDialog(this);
        initView();
    }

    private void initView() {
        retur.setOnClickListener(this);
        search_ly.setOnClickListener(this);
        if (getIntent() != null) {
            pos = getIntent().getStringExtra("pos");
        }
        if (pos.equals("0")) {
            title_center.setText("苦力");
            search_ly.setVisibility(View.VISIBLE);
            mApi = Constant.COOLIE_INDEX;
        } else if (pos.equals("1")) {
            title_center.setText("干洗");
            search_ly.setVisibility(View.VISIBLE);
            mApi = Constant.CLEANING_INDEX;
        } else if (pos.equals("2")) {
            title_center.setText("开锁");
            search_ly.setVisibility(View.VISIBLE);
            mApi = Constant.UNLOCKING_INDEX;
        } else if (pos.equals("3")) {
            title_center.setText("家政");
            search_ly.setVisibility(View.VISIBLE);
            mApi = Constant.HOUSEKEEPING_MERCHANT;
        } else {
            title_center.setText("果蔬");
            search_ly.setVisibility(View.VISIBLE);
            mApi = Constant.VAFETABLES_INDEX;
        }
        mListView = mPullToRefreshListView.getRefreshableView();
        // 显示头部出现分割线
        mListView.setHeaderDividersEnabled(false);
        // 禁止底部出现分割线
        mListView.setFooterDividersEnabled(false);
        mListView.setDivider(new ColorDrawable(Color.TRANSPARENT));
        mListView.setDividerHeight(0);
        mPullToRefreshListView.setOnRefreshListener(refreshListener);
        getData();
    }

    /**
     * 获取数据
     */
    private void getData() {
        String pos = String.valueOf(mPageIndex);
        String lat = String.valueOf(MyApplication.getInstance().getLatitude());
        String lng = String.valueOf((MyApplication.getInstance().getLongitude()));
        CleanBusiness.postBuyACarFragmentModel(lat, lng, pos, mApi, this);
    }

    /**
     * 加载数据
     */
    private void LoadData() {
        if (mAdapter == null) {
            mAdapter = new CleaningActivityAdapter(this, mListBean, pos);
            mListView.setAdapter(mAdapter);
        } else {
            mAdapter.addItem(mListBean);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.retur:
                finish();
                break;
            case R.id.search_ly:
                startActivity(new Intent(CleaningActivity.this, SearchActivity.class));
                break;
        }
    }

    /**
     * 请求数据
     *
     * @param result
     */
    @Override
    public void onSuccess(String result) {
        mDialog.dismiss();
        if (result != null) {
            mListBean = CleanBusiness.parseToBuyACarFragmentModel(result);
            if (mListBean != null && mListBean.size() > 0) {
                LoadData();
            }
        }
    }

    @Override
    public void onError(Throwable ex, boolean isOnCallback) {
        mDialog.dismiss();
    }

    @Override
    public void onCancelled(CancelledException cex) {
        mDialog.dismiss();
        mPullToRefreshListView.onPullDownRefreshComplete();
        mPullToRefreshListView.onPullUpRefreshComplete();
        ToastUtils.Toastshow(this, getResources().getString(R.string.server));
    }

    @Override
    public void onFinished() {

    }


    /**
     * 上拉刷新，下拉获取更多
     */
    PullToRefreshBase.OnRefreshListener refreshListener = new PullToRefreshBase.OnRefreshListener() {

        @Override
        public void onPullDownToRefresh(PullToRefreshBase refreshView) {
            mIsStart = true;
            mPageIndex = 1;
            getData();
        }

        @Override
        public void onPullUpToRefresh(PullToRefreshBase refreshView) {
            mIsStart = false;
            mPageIndex++;
            LoadData();
        }
    };
}

